<template>
  <div>
    <h3>{{id?'编辑':'新建'}}地址</h3>
    <el-form label-width="60px" @submit.native.prevent="save">
      <el-form-item label="名称">
        <el-input v-model="model.name"></el-input>
      </el-form-item>
      <el-form-item label="code">
        <el-input v-model="model.code"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-input v-model="model.state"></el-input>
      </el-form-item>
      <el-form-item style="margin-top:10px">
        <el-button type="primary" native-type="submit">{{id?'更新':'创建'}}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: {}
  },
  data() {
    return {
      model: {
        token: sessionStorage.token,
        table: "address",
        name: "",
        code: "",
        state: 1
      }
    };
  },
  methods: {
    fetch() {
      const info = {
        cmd: 0,
        val: {
          token: sessionStorage.token,
          table: "address",
          ID: this.id
        }
      };
      this.$axios.post("q.php", info).then(res => {
        if (res.data.result) {
          this.model.name = res.data.result[0].name;
          this.model.code = res.data.result[0].code;
          this.model.state = res.data.result[0].state;
        }
      });
    },
    // Form表单保存
    mesIF(res) {
      if (res.data) {
        this.$message({
          type: "success",
          message: "添加成功"
        });
        this.$router.push("/address/list");
      } else {
        this.$message({
          type: "danger",
          message: "添加失败"
        });
      }
    },
    save() {
      const info = {
        cmd: 1,
        val: this.model
      };
      if (this.id) {
        info.val.ID = this.id;
        this.$axios.post("q.php", info).then(res => {
          this.mesIF(res);
          console.log(res.data);
        });
      } else {
        info.cmd = 2;
        this.$axios.post("q.php", info).then(res => {
          this.mesIF(res);
        });
      }
    }
  },
  created() {
    this.id && this.fetch();
  }
};
</script>

<style lang="scss">
.avatar-uploader {
  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    &:hover {
      border-color: #409eff;
    }
  }
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  min-width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
}
.avatar {
  width: 148px;
  height: 148px;
  display: block;
}
</style>